//
//  TakePhotoView.swift
//  ezyform
//
//  Created by Moennig Q on 15/4/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class TakePhotoView: UIView {
    var photoImageViewClickClosure: (()->Void)?
    let topBackView = UIView()
    lazy var titleLabel: BaseLabel = {
        let label = BaseLabel()
        label.textColor = .white
        label.setFont(R.font.interBold(size: 16))
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    lazy var swithCamaraBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "change_camera"), for: UIControl.State.normal)
        return btn
    }()
    //backview before taking photo
    lazy var beforeBackView: UIView = {
        let view = UIView()
        return view
    }()
    lazy var returnBtn: UIButton = {
        let btn = UIButton()
        return btn
    }()
    
    lazy var takePhotoBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "service_myjob_takephoto_btn_takephoto"), for: UIControl.State.normal)
        return btn
    }()
    
    lazy var photoImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "service_myjob_takephoto_photo_icon"))
        
        return imageView
    }()
    
    lazy var photoCntLabel: BaseLabel = {
        let label = BaseLabel()
        label.textColor = .white
        label.setFont(R.font.interRegular(size: 12))
        label.backgroundColor = UIColor(hexStr: "#ED3E3E")
        label.textAlignment = .center
        label.layer.cornerRadius = 9
        label.clipsToBounds = true
        return label
    }()
    
    lazy var noAvailableDeviceLabel: BaseLabel = {
        let label = BaseLabel()
        label.text = R.string.localizable.noCameraAvailable()
        label.textColor = .red
        label.setFont(R.font.sfProTextSemibold(size: 20))
        label.backgroundColor = .clear
        return label
    }()
    let maskLayer = UIView()
    //IB init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    //纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpView()
    }
    private func setUpView() {
        //蒙层用户动画效果
        maskLayer.backgroundColor = .black
        maskLayer.alpha = 0
        maskLayer.isUserInteractionEnabled = false
        self.addSubview(maskLayer)
        maskLayer.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        //
        topBackView.backgroundColor = .black
        self.addSubview(topBackView)
        topBackView.snp.makeConstraints({ make in
            make.left.top.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(88)
        })
        //title
        topBackView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(48)
            make.height.greaterThanOrEqualTo(20)
            make.bottom.equalTo(-15)
        }
        //switch camera
        topBackView.addSubview(swithCamaraBtn)
        swithCamaraBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right)
            make.right.equalTo(-8)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        //
        //没有有效设备的提示信息
        self.addSubview(noAvailableDeviceLabel)
        noAvailableDeviceLabel.snp.makeConstraints { (make) in
            make.width.height.greaterThanOrEqualTo(0)
            make.center.equalToSuperview()
        }
        beforeBackView.backgroundColor = UIColor(hexStr: Constant.ThemeColorHexStr)
        self.addSubview(beforeBackView)
        beforeBackView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        
        //2.1 take photo btn
        beforeBackView.addSubview(takePhotoBtn)
        takePhotoBtn.snp.makeConstraints { (make) in
            make.top.equalTo(15)
            make.width.height.equalTo(50)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(self.safeAreaLayoutGuide.snp.bottom).offset(Constant.BottomMargin)
        }
        //2.2 return btn
        returnBtn.setImage(UIImage(named: "service_myjob_takephoto_btn_return"), for: UIControl.State.normal)
        beforeBackView.addSubview(returnBtn)
        returnBtn.snp.makeConstraints { (make) in
            make.left.equalTo(26)
            make.width.height.equalTo(32)
            make.centerY.equalTo(takePhotoBtn)
        }
        //2.3 image view
        beforeBackView.addSubview(photoImageView)
        photoImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-26)
            make.width.height.equalTo(32)
            make.centerY.equalTo(takePhotoBtn)
        }
        //2.4 photo cnt label
        beforeBackView.addSubview(photoCntLabel)
        photoCntLabel.snp.makeConstraints { (make) in
            make.left.equalTo(photoImageView.snp.right).offset(-15)
            make.bottom.equalTo(photoImageView.snp.top).offset(15)
            make.height.equalTo(18)
            make.width.greaterThanOrEqualTo(18)
        }
        photoImageView.isUserInteractionEnabled = true
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(photoImageViewClick))
        photoImageView.addGestureRecognizer(tapGesture)
    }
    @objc func photoImageViewClick(){
        self.photoImageViewClickClosure?()
    }
    //MARK: - 设置正常拍照的UI
    func setTakePhotoTitle(_ title: String){
        self.titleLabel.text = title
    }
    
    //防抖处理，或连续快速点击导致相机不可用
    func disableTakePhotoBtn(){
        self.takePhotoBtn.isUserInteractionEnabled = false
    }
    func enableTakePhotoBtn(){
        UIView.animate(withDuration: 1) {
            self.takePhotoBtn.isUserInteractionEnabled = true
        }
    }

}
